package com.yugao.fintech.draper.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yugao.fintech.draper.admin.convert.UserConvert;
import com.yugao.fintech.draper.admin.convert.SocialUserConvert;
import com.yugao.fintech.draper.admin.entity.SocialUser;
import com.yugao.fintech.draper.admin.enums.SocialAppTypeEnums;
import com.yugao.fintech.draper.admin.mapper.SocialUserMapper;
import com.yugao.fintech.draper.admin.service.SocialOperationService;
import com.yugao.fintech.draper.admin.service.SocialUserService;
import com.yugao.fintech.draper.auth.dto.SocialUserDTO;
import com.yugao.fintech.draper.admin.param.SocialUserBindInfo;
import com.yugao.fintech.draper.auth.dto.UserInfoVO;
import com.yugao.fintech.draper.security.SecurityUser;
import com.yugao.fintech.draper.security.util.SecurityUserUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;
@Slf4j
@Service
@RequiredArgsConstructor
public class SocialUserServiceImpl implements SocialUserService, SocialOperationService {
    @Autowired
    private SocialUserMapper socialUserMapper;

    @Override
    public Boolean addSysUsersGitee(SocialUserDTO socialUserDTO) {
        return socialUserMapper.insert(SocialUserConvert.INSTANCE.convert(socialUserDTO)) > 0;
    }

    @Override
    public SocialUserDTO findById(Long id) {
        SocialUser socialUser = socialUserMapper.selectById(id);
        return SocialUserConvert.INSTANCE.convert(socialUser);
    }

    @Override
    public SocialUserBindInfo getBindInfo(Long userId) {
        SocialUser socialUser = socialUserMapper.selectOne(Wrappers.<SocialUser>lambdaQuery().eq(SocialUser::getUsersId, userId));
        SocialUserBindInfo.SocialUserBindInfoBuilder builder = SocialUserBindInfo.builder();
        if (Objects.nonNull(socialUser)) {
            builder.bind(true).username(socialUser.getName()).avatar(socialUser.getAvatarUrl());
        }
        return builder.build();
    }

    /**
     * @param appId 应用id
     * @param id    gitee用户唯一标识
     * @return
     */
    @Override
    public SocialUserDTO getByAppidAndId(String appId, Integer id) {
        SocialUser socialUser = socialUserMapper.selectOne(Wrappers.<SocialUser>lambdaQuery().eq(SocialUser::getAppid, appId).eq(SocialUser::getId, id));
        return SocialUserConvert.INSTANCE.convert(socialUser);
    }

    /**
     * 绑定用户
     *
     * @param socialUserDTO
     */
    @Override
    public Boolean binding(SocialUserDTO socialUserDTO) {
        SocialUser convert = SocialUserConvert.INSTANCE.convert(socialUserDTO);
        LambdaUpdateWrapper<SocialUser> wrapper = Wrappers.<SocialUser>lambdaUpdate()
                .eq(SocialUser::getId, socialUserDTO.getId())
                .eq(SocialUser::getAppid, socialUserDTO.getAppid())
                .set(SocialUser::getUsersId, socialUserDTO.getUsersId())
                .set(SocialUser::getBindingDate, LocalDateTime.now());
        return socialUserMapper.update(convert, wrapper) > 0;
    }

    /**
     * @param appId
     * @param id
     * @return
     */
    @Override
    public UserInfoVO getUser(String appId, Integer id) {
        return UserConvert.INSTANCE.convert(socialUserMapper.getUser(appId, id));
    }

    /**
     * 更新
     */
    @Override
    public Boolean update(SocialUserDTO socialUserDTO) {
        return socialUserMapper.updateById(SocialUserConvert.INSTANCE.convert(socialUserDTO)) > 0;
    }

    /**
     * 解除绑定
     */
    @Override
    public void unbind() {
        SecurityUser user = SecurityUserUtil.getCurrentUser();
        SocialUser socialUser = new SocialUser();
        socialUser.setUsersId(user.getUserId());
        socialUserMapper.update(socialUser, Wrappers.<SocialUser>lambdaUpdate().eq(SocialUser::getUsersId, user.getUserId()).set(SocialUser::getUsersId, null));
    }

    @Override
    public String support() {
        return SocialAppTypeEnums.GITEE.getValue();
    }

}